package com.gemptc.service.iphone;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import com.gemptc.bean.ReplyTravelsBean;
import com.gemptc.daoimp.ReplyTravelsImp;

/**
 * Servlet implementation class ReplyTravles
 */
@WebServlet("/ReplyTravles")
public class ReplyTravles extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ReplyTravles() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter printWriter = response.getWriter();
		ReplyTravelsImp replyTravelsImp = new ReplyTravelsImp();
		String rt_id = null, user_id, reply_userid, t_id, r_time, r_content, r_rank, user_nickname, reply_usernickname; // 游记回复表id
		System.out.println("信息");
		String Value = request.getParameter("key");
		switch (Integer.parseInt(Value)) {
		case 1:
			t_id = request.getParameter("t_id");
			List<ReplyTravelsBean> list = replyTravelsImp
					.allReplyTravelsBeans(Integer.parseInt(t_id));
			JSONArray jsonArray = JSONArray.fromObject(list);
			printWriter.write(jsonArray.toString());
			printWriter.close();
			break;
		case 2:
			t_id = request.getParameter("t_id");
			user_id = request.getParameter("user_id");
			reply_userid = request.getParameter("reply_userid");
			r_content = request.getParameter("r_content");
			r_rank = request.getParameter("r_rank");
			Date currentTime = new Date();
			r_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
					.format(currentTime);
			System.out.println(user_id+"--user_id--"+reply_userid+"--reply_userid--"+ t_id+"--t_id--"+ r_time+"--r_time--"+ r_content+"--r_content--"+ r_rank+"--r_rank--");
			boolean flag = replyTravelsImp.insertTravels(
					Integer.parseInt(user_id), Integer.parseInt(reply_userid),
					Integer.parseInt(t_id), r_content, r_time,
					Integer.parseInt(r_rank));
			if (flag) {
				printWriter.write("success");
				
			} else {
				printWriter.write("error");
			}
			break;
		case 3:
			rt_id = request.getParameter("rt_id");
			t_id = request.getParameter("t_id");
			user_id = request.getParameter("user_id");
			reply_userid = request.getParameter("reply_userid");
			r_content = request.getParameter("r_content");
			r_rank = request.getParameter("r_rank");
			Date currentTime1 = new Date();
			r_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
					.format(currentTime1);
			boolean flag1 = replyTravelsImp.TwoinsertTravels(
					Integer.parseInt(rt_id), Integer.parseInt(user_id),
					Integer.parseInt(reply_userid), Integer.parseInt(t_id),
					r_content, r_time, Integer.parseInt(r_rank));
			if (flag1) {
				printWriter.write("success");
			} else {
				printWriter.write("error");
			}
			break;
		default:
			break;
		}
	}
}
